home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / util / arc / CheckX.lha / CheckX / sources / CheckX.c next >
Encoding:
C/C++ Source or Header  |  2001-07-13  |  51.4 KB  |  1,745 lines

  1. #define NAME         "CheckX"
  2. #define REVISION     "85"
  3. #define DISTRIBUTION "(Freeware) "
  4. #define DATE         "12.07.2001"
  5. #define VERSION      "1"
  6. #define AUTHOR       "by Dirk Stöcker <stoecker@epost.de>"
  7.  
  8. /*
  9. This program scans for crunched, linked files and archived files. It
  10. decrunches them and saves the result files to another directory-tree.
  11. The scanning routines are recursive and thus check really all stuff.
  12.  
  13. The program must be compiled and linked without startup-code. You can set
  14. the pure file protection bit and make it resident, because it is multi-
  15. reentrant (no global variables, except library bases).
  16. */
  17.  
  18. /* Programmheader
  19.  
  20.     Name:        CheckX
  21.     Author:        SDI
  22.     Distribution:    Freeware
  23.     Description:    scans, decrunches and dearchives files
  24.     Compileropts:    -
  25.     Linkeropts:    -l xpkmaster xadmaster amiga -gsi
  26.  
  27.  1.0   14.12.96 : first Version
  28.  1.1   28.12.96 : moved PassRequest into xpkmaster.library
  29.  1.2   12.02.97 : now also decrunches Exe-Files
  30.  1.3   15.06.97 : added length output as test
  31.  1.4   21.11.97 : renamed from Decrunch, got really new program
  32.  1.5   22.11.97 : bug-fixes
  33.  1.6   29.11.97 : added unarchiving feature
  34.  1.7   30.11.97 : bug-fixes
  35.  1.8   06.12.97 : xpkmaster.library now only required with ASKPWD option
  36.  1.9   07.12.97 : Added archive copy for weird archive names, better error
  37.     codes
  38.  1.10  08.12.97 : fixed error codes a bit
  39.  1.11  11.12.97 : disabled DOS requests, added Zip-Archives, added TaskID
  40.     to temporary filenames
  41.  1.12  12.12.97 : added Arc, ZOO and LhASFX archives
  42.  1.13  13.12.97 : fixed Arc recognition
  43.  1.14  19.12.97 : deletes copied arc before scan
  44.  1.15  22.12.97 : crunched archives are unarchived correctly now
  45.  1.16  02.01.98 : opens dos.library itself, no startup-code required
  46.  1.17  23.01.98 : added disk crunchers
  47.  1.18  24.01.98 : some fixes
  48.  1.19  01.02.98 : little bug-fix in argument-option use
  49.  1.20  06.02.98 : better error output, added automount
  50.  1.21  08.02.98 : little bug-fix
  51.  1.22  10.02.98 : fixed archive copy conditions, added PRINTALL
  52.  1.23  12.02.98 : fixed help text, bug fixes with unlinking and FreeMem
  53.  1.24  13.02.98 : added unstripping
  54.  1.25  04.03.98 : added PRINTEXEC
  55.  1.26  13.03.98 : added high density DMS support
  56.  1.27  19.03.98 : added LOUD keyword
  57.  1.28  23.03.98 : RDx no longer depends on archive depth, but on dddepth
  58.  1.29  10.04.98 : bug fixes
  59.  1.30  26.04.98 : bug fixes
  60.  1.31  09.05.98 : now uses no longer adress 4 for SysBase
  61.  1.32  31.05.98 : better output
  62.  1.33  04.06.98 : added HEADER addition for address files
  63.  1.34  08.08.98 : bug fix with SAVE
  64.  1.35  24.09.98 : added xvs.library virus checks
  65.  1.36  18.10.98 : xvs is opened global and only once
  66.  1.37  30.10.98 : renamed from CheckXFD
  67.  1.38  11.11.98 : fixed format drive bug using a delay and an error report
  68.  1.39  14.11.98 : format error with AUTOMOUNT removed
  69.  1.40  16.11.98 : removed HEADER addition stuff
  70.  1.41  18.11.98 : better RDx: access and mount
  71.  1.42  23.11.98 : fixed bug with hunk stripping
  72.  1.43  29.12.98 : now prints an error, when virus detection is turned off,
  73.     added time calculation and output
  74.  1.44  31.12.98 : little bug fix
  75.  1.45  06.02.99 : bug fixes, added xadmaster.library stuff, removed LOUD
  76.     and internal DMS call
  77.  1.46  09.02.99 : removed internal LZX call
  78.  1.47  11.02.99 : now uses assembler startcode allocating a bigger stack
  79.  1.48  14.02.99 : bug-fixes for nocylinder archivers (PackDev)
  80.  1.49  16.02.99 : fixed strip option (don't know, where the error was :-)
  81.  1.50  21.02.99 : removed internal Zoom call and disk-archiver stuff
  82.  1.51  22.02.99 : fixed archiver call for remaining non-XAD archivers, added
  83.     empty file check
  84.  1.52  23.02.99 : fixed StartCode return value
  85.  1.53  24.02.99 : forgot empty check for archived files
  86.  1.54  08.03.99 : old archiver calling did not work, when file was XAD
  87.     unarchived
  88.  1.55  26.03.99 : prints error, when logfile cannot be created
  89.  1.56  30.03.99 : added bootblock scanning for unarchived disks
  90.  1.57  16.05.99 : bug fix with unlinked file save
  91.  1.58  17.07.99 : removed internal LhA Support
  92.  1.59  30.07.99 : bug fix with file name prints
  93.  1.60  03.08.99 : added DEBUG option
  94.  1.61  05.08.99 : added XVS SelfTest and MemoryTest
  95.  1.62  19.08.99 : added error summary, again fixed name problem
  96.  1.63  14.09.99 : solved big memory loss problem (did not free xadArchiveInfo),
  97.     added logfile comment
  98.  1.64  17.10.99 : removed external archiver calls, added QUIET
  99.  1.65  24.11.99 : now sets nice return values
  100.  1.66  26.11.99 : tries reading again before giving read error
  101.  1.67  03.12.99 : bug fix in name printing
  102.  1.68  04.12.99 : tries again opening files.
  103.  1.69  16.12.99 : added NOSILENT Option
  104.  1.70  15.01.00 : files from disc archives are extracted directly now,
  105.     removed double read tries
  106.  1.71  16.01.00 : fixed error messages for disk unarchiving
  107.  1.72  12.02.00 : added support for ADF images
  108.  1.73  13.02.00 : bug fix for disk archives
  109.  1.74  09.03.00 : now detects XADFIF_NOFILENAME and XADFIF_NOUNCRUNCHSIZE
  110.  1.75  05.04.00 : fixed bug with XADFIF_NOFILENAME
  111.  1.76  26.04.00 : added size checkout for XADFIF_NOUNCRUNCHSIZE
  112.  1.77  05.06.00 : added support for multiple filesystems, removed NODOS and
  113.     diskerr errors, always prints FS-type now.
  114.  1.78  24.07.00 : bug fix
  115.  1.79  27.07.00 : bug fix with multi FS support
  116.  1.80  14.09.00 : added SAVEALL keyword
  117.  1.81  25.03.01 : added support for crypted archives and large archive scan
  118.  1.82  05.04.01 : fixed Enforcer hit
  119.  1.83  08.04.01 : empty file got warning
  120.  1.84  18.04.01 : added texts in case libraries could not be opened
  121.  1.85  12.07.01 : added sector checks, reduced final file size
  122. */
  123.  
  124. #include <proto/exec.h>
  125. #include <proto/dos.h>
  126. #include <proto/xfdmaster.h>
  127. #include <proto/xadmaster.h>
  128. #include <proto/xpkmaster.h>
  129. #include <proto/intuition.h>
  130. #include <proto/xvs.h>
  131. #include <proto/utility.h>
  132. #include <libraries/xfdmaster.h>
  133. #include <dos/dostags.h>
  134. #include <dos/doshunks.h>
  135. #include <dos/filehandler.h>
  136. #include <exec/memory.h>
  137. #include "SDI_compiler.h"
  138.  
  139. #define PARAM   "FROM,LOG,SAVE/K,ALL/S,ASKPWD/S,PRINTALL/S,PRINTEXEC/S," \
  140.         "NODECRUNCH/S,NOUNLINK/S,NOUNARCHIVE/S,NOUNTRACK/S,"     \
  141.         "NOSILENT/S,NOSTRIP/S,NOVIRUS/S,DEBUG/S,QUIET/S,"        \
  142.         "SAVEALL/S,SECTORCHECK/S"
  143.  
  144. #define version "$VER: " NAME " " VERSION "." REVISION " (" DATE ") " DISTRIBUTION AUTHOR
  145.  
  146. void RawPutChar(ULONG c);
  147.  
  148. #ifdef __SASC
  149.   #pragma libcall SysBase RawPutChar 204 001
  150. #elif defined(__GNUC__)
  151.   #define RawPutChar(c) LP1NR(204, RawPutChar, ULONG, c, d0, , SysBase)
  152. #else
  153.   #pragma amicall(SysBase,0x204,RawPutChar(d0))
  154. #endif
  155.  
  156. struct FileData {
  157.   struct CrunchMemList * fd_MemList;
  158. #ifdef __SASC
  159.   struct xfdMasterBase * fd_xfdMasterBase;
  160.   struct xvsBase *     fd_xvsBase;
  161.   struct xadMasterBase * fd_xadMasterBase;
  162.   struct DosLibrary *     fd_DOSBase;
  163.   struct ExecBase *     fd_SysBase;
  164. #endif
  165.   STRPTR         fd_Name;
  166.   STRPTR                 fd_Memory; /* for bootblock checks */
  167.   BPTR           fd_LogFileFH;
  168.   BPTR             fd_ArcFileFH;
  169.   ULONG           fd_SaveDirL;
  170.   ULONG             fd_Flags;
  171.   ULONG             fd_NumVirus;
  172.   ULONG                  fd_NumSectors;
  173.   ULONG             fd_CHKXErrors;
  174.   ULONG                  fd_CHKXWarnings; /* not printed yet */
  175.   ULONG             fd_XFDErrors;
  176.   ULONG             fd_XADErrors;
  177.   ULONG             fd_CorruptedArchives;
  178.   BYTE           fd_RecurseDepth;
  179.   UBYTE           fd_LinkNum;
  180.   UBYTE             fd_ArchiveDepth;
  181. };
  182.  
  183. typedef struct xadMasterBase XADMASTERBASE;
  184. typedef struct xfdMasterBase XFDMASTERBASE;
  185. typedef struct xvsBase XVSBASE;
  186.  
  187. #ifdef __SASC
  188.   static struct ExecBase *sysbase;
  189.   #define XpkBase    xpkbase
  190.   #define ASSIGN_XPK
  191.   #define IntuitionBase    intuitionbase
  192.   #define ASSIGN_INT
  193.   #define UtilityBase    utilitybase
  194.   #define ASSIGN_UTIL
  195.   #define xfdMasterBase    fd->fd_xfdMasterBase
  196.   #define ASSIGN_SYS    sysbase = fd->fd_SysBase = (*((struct ExecBase **) 4));
  197.   #define SysBase       fd->fd_SysBase
  198.   #define DOSBase       fd->fd_DOSBase
  199.   #define ASSIGN_DOS    DOSBase = dosbase;
  200.   #define xvsBase    fd->fd_xvsBase
  201.   #define xadMasterBase    fd->fd_xadMasterBase
  202. #else
  203.   struct Library *     XpkBase;
  204.   struct IntuitionBase * IntuitionBase;
  205.   struct UtilityBase *   UtilityBase;
  206.   #define ASSIGN_XPK     XpkBase        = xpkbase;
  207.   #define ASSIGN_INT     IntuitionBase  = intuitionbase;
  208.   #define ASSIGN_UTIL     UtilityBase    = utilitybase;
  209.   #define ASSIGN_SYS     SysBase        = (*((struct ExecBase **) 4));
  210.   #define ASSIGN_DOS     DOSBase        = dosbase;
  211.   struct xfdMasterBase * xfdMasterBase;
  212.   struct xvsBase *     xvsBase;
  213.   struct xadMasterBase * xadMasterBase;
  214.   struct DosLibrary *     DOSBase;
  215.   struct ExecBase *     SysBase;
  216. #endif
  217.  
  218. struct Args {
  219.   STRPTR from;
  220.   STRPTR log;
  221.   STRPTR save;
  222.   ULONG  all;
  223.   ULONG  askpwd;
  224.   ULONG  printall;
  225.   ULONG  printexec;
  226.   ULONG  nodecrunch;
  227.   ULONG  nounlink;
  228.   ULONG  nounarchive;
  229.   ULONG  nountrack;
  230.   ULONG  nosilent;
  231.   ULONG  nostrip;
  232.   ULONG  novirus;
  233.   ULONG  debug;
  234.   ULONG     quiet;
  235.   ULONG  saveall;
  236.   ULONG  sectorcheck;
  237. };
  238.  
  239. struct CrunchMemList {
  240.   struct CrunchMemList * cml_Next;
  241.   APTR             cml_MemoryRegion;
  242.   ULONG             cml_MemorySize;
  243. };
  244.  
  245. #define CHECKXFLAG_SAVE            (1<< 0)
  246. #define CHECKXFLAG_ALL            (1<< 1)
  247. #define CHECKXFLAG_ASKPWD        (1<< 2)
  248. #define CHECKXFLAG_PRINTALL        (1<< 3)
  249. #define CHECKXFLAG_PRINTEXEC        (1<< 4)
  250. #define CHECKXFLAG_NODECRUNCH        (1<< 5)
  251. #define CHECKXFLAG_NOUNLINK        (1<< 6)
  252. #define CHECKXFLAG_NOUNARCHIVE        (1<< 7)
  253. #define CHECKXFLAG_NOUNTRACK        (1<< 8)
  254. #define CHECKXFLAG_NOSTRIP        (1<< 9)
  255. #define CHECKXFLAG_DEBUG        (1<<10)
  256. #define CHECKXFLAG_QUIET        (1<<11)
  257. #define CHECKXFLAG_SAVEALL        (1<<12)
  258. #define CHECKXFLAG_SECTORCHECK        (1<<13)
  259.  
  260. #define CHECKXFLAG_XVSLIB        (1<<20)
  261.  
  262. #define CHKXCALLFLAGS    (CHECKXFLAG_SAVE|CHECKXFLAG_ALL|        \
  263.              CHECKXFLAG_ASKPWD|CHECKXFLAG_PRINTALL|        \
  264.              CHECKXFLAG_PRINTEXEC|CHECKXFLAG_NODECRUNCH|    \
  265.              CHECKXFLAG_NOUNLINK|CHECKXFLAG_NOUNARCHIVE|    \
  266.              CHECKXFLAG_NOUNTRACK|CHECKXFLAG_NOSTRIP|    \
  267.              CHECKXFLAG_DEBUG|CHECKXFLAG_QUIET|        \
  268.              CHECKXFLAG_XVSLIB|CHECKXFLAG_SAVEALL|        \
  269.              CHECKXFLAG_SECTORCHECK)
  270.  
  271. #define CHKXSAVEFLAGS    (CHECKXFLAG_LINKED|CHECKXFLAG_CRUNCHED|        \
  272.              CHECKXFLAG_STRIPPED|CHECKXFLAG_SAVEALL)
  273.  
  274. #define CHECKXFLAG_NAMEPRINTED        (1<<26)
  275. #define CHECKXFLAG_CRUNCHED        (1<<27)
  276. #define CHECKXFLAG_LINKED        (1<<28)
  277. #define CHECKXFLAG_ADDRESS        (1<<29)
  278. #define CHECKXFLAG_NOFREEMEM        (1<<30)
  279. #define CHECKXFLAG_STRIPPED        (1<<31)
  280.  
  281. #define CHXWARN_OFFSET        30
  282. #define XADERR_OFFSET        0x100
  283. #define XFDERR_OFFSET        0x200
  284.  
  285. #define CHKXERR_NOMEMORY    1
  286. #define CHKXERR_EXAMINEERR    2
  287. #define CHKXERR_OPENERR        3
  288. #define CHKXERR_READ        4
  289. #define CHKXERR_SCANERR        5
  290. #define CHKXERR_BREAK        6
  291. #define CHKXERR_OPENDIR        7
  292. #define CHKXERR_RESOURCE    8
  293. #define CHKXERR_NOBOOTVIRUS    9
  294. #define CHKXERR_WRITE        10
  295. #define CHKXERR_NOVIRUS        11
  296. #define CHKXERR_NOMEMORYARC    12
  297. #define CHKXERR_NOSECTOR        13
  298.  
  299. #define CHKXWARN_NOVIRUS    31
  300. #define CHKXWARN_XVSSELFTEST    32
  301. #define    CHKXWARN_MEMVIRUS    33
  302. #define CHKXWARN_EMPTY        34
  303.  
  304. static void SetLogComment(struct FileData *, STRPTR);
  305. static LONG DoDirectoryScan(struct FileData *, STRPTR, STRPTR);
  306. static LONG DoFileOpen(struct FileData *);
  307. static LONG DoGetVirus(struct FileData *, APTR, ULONG);
  308. static LONG DoFileUnArchive(struct FileData *, APTR, ULONG);
  309. static LONG DoFileUnLink(struct FileData *, APTR, ULONG);
  310. static LONG DoFileUnCrunch(struct FileData *, APTR, ULONG);
  311. static LONG DoFileStrip(struct FileData *, APTR, ULONG);
  312. static void PrintCHKXFile(struct FileData *);
  313. static void PrintCHKXErr(struct FileData *, LONG);
  314. static void PrintCHKXTxt(struct FileData *, STRPTR, ...);
  315. static LONG AddCrunchMemList(struct FileData *, APTR, ULONG);
  316. static void FreeCrunchMemList(struct FileData *, APTR);
  317. static LONG SaveUncrFile(struct FileData *, APTR, ULONG);
  318. static ULONG OpenParentDir(struct FileData *);
  319. static ULONG OpenNewDir(struct FileData *, STRPTR);
  320. static LONG DoSectorCheck(struct FileData *, APTR, ULONG, ULONG);
  321. static const struct Hook breakhook;
  322. static void clear(APTR, ULONG);
  323. static void SPrintF(STRPTR buf, STRPTR format, ...);
  324. static ASM(void) KPutC(REG(d0, ULONG c));
  325. static void KPrintf(STRPTR fmt, ...);
  326.  
  327. /* All memory regions must be in mem list. All unneeded memory must be freed
  328.    as fast as possible (after unlinking, decrunching), as well as the
  329.    MemoryList structure.
  330.    
  331.    The program has a loop like scan routine system, which is called for
  332.    every file:
  333.  
  334.    A) Scan files, directories and sub directories and call following for
  335.       every file:
  336.    1) Check for viruses.
  337.    2) Test if it is an archive. When yes decrunch and start for every file
  338.       with point 1.
  339.    3) Test if file is linked. When yes unlink and call point 1 for both
  340.       parts.
  341.    4) Test if file is crunched. When yes, decrunch and start again with
  342.       point 1.
  343.    5) Try stripping useless stuff. When successful start with point 1 again.
  344.    6) Possibly save file (with SAVE option) or end loop here.
  345. */
  346.  
  347. /* main routine, do argument parsing */
  348. LONG start(void)
  349. {
  350.   LONG error = RETURN_FAIL;
  351.   struct FileData fd[1];
  352.   struct DosLibrary *dosbase;
  353.   struct Process *task;
  354.  
  355.   clear((STRPTR)fd, sizeof(struct FileData));
  356.   ASSIGN_SYS
  357.  
  358.   /* test for WB and reply startup-message */
  359.   if(!(task = (struct Process *) FindTask(0))->pr_CLI)
  360.   {
  361.     WaitPort(&task->pr_MsgPort);
  362.     Forbid();
  363.     ReplyMsg(GetMsg(&task->pr_MsgPort));
  364.     return RETURN_FAIL;
  365.   }
  366.  
  367.   if((dosbase = (struct DosLibrary *) OpenLibrary("dos.library", 37)))
  368.   {
  369.     XFDMASTERBASE * xfdmasterbase;
  370.  
  371.     ASSIGN_DOS
  372.     if((xfdmasterbase = (XFDMASTERBASE *) OpenLibrary("xfdmaster.library", 39)))
  373.     {
  374.       XADMASTERBASE *xadmasterbase;
  375.  
  376.       xfdMasterBase = xfdmasterbase;
  377.       if((xadmasterbase = (XADMASTERBASE *) OpenLibrary("xadmaster.library", 10)))
  378.       {
  379.         struct Args Args;
  380.         struct RDArgs *rda;
  381.  
  382.     xadMasterBase = xadmasterbase;
  383.         clear((STRPTR)(&Args), sizeof(struct Args));
  384.  
  385.         if((rda = (struct RDArgs *) AllocDosObject(DOS_RDARGS, 0)))
  386.         {
  387.           rda->RDA_ExtHelp = version; /* is removed by optimizer */
  388.           rda->RDA_ExtHelp =
  389.           "FROM        source file or directory - may contain patterns\n"
  390.           "LOG         log file name\n"
  391.           "SAVE        directory, where decrunched files are saved\n"
  392.           "ALL         scan deep into directories\n"
  393.           "ASKPWD      ask for password when needed (needs xpkmaster.library)\n"
  394.       "PRINTALL    print all filenames\n"
  395.           "PRINTEXEC   print names of all executable files\n"
  396.           "NODECRUNCH  do not decrunch files with xfdmaster\n"
  397.           "NOUNLINK    do not unlink files with xfdmaster\n"
  398.           "NOUNARCHIVE do not unarchive file archives\n"
  399.           "NOUNTRACK   do not unarchive track archives\n"
  400.           "NOSILENT    do not disable dos requests\n"
  401.           "NOSTRIP     do not strip useless hunks\n"
  402.           "NOVIRUS     do not scan with xvs.library for viruses\n"
  403.           "DEBUG       also output texts to serial debug engine\n"
  404.           "QUIET       do not output texts to console\n"
  405.           "SAVEALL     saves all files (also uncrunched) except address files\n"
  406.           "SECTORCHECK checks the files for virus infected sectors\n";
  407.  
  408.           if(ReadArgs(PARAM, (LONG *) &Args, rda))
  409.           {
  410.             ULONG flags = 0, log = 0;
  411.             XVSBASE *xvsbase = 0;
  412.  
  413.             if(!Args.from)        Args.from = "#?";
  414.             if(Args.all)        flags |= CHECKXFLAG_ALL;
  415.             if(Args.save)        flags |= CHECKXFLAG_SAVE;
  416.             if(Args.saveall)        flags |= CHECKXFLAG_SAVEALL;
  417.             if(Args.askpwd)        flags |= CHECKXFLAG_ASKPWD;
  418.         if(Args.printall)        flags |= CHECKXFLAG_PRINTALL;
  419.         if(Args.printexec)        flags |= CHECKXFLAG_PRINTEXEC;
  420.             if(Args.nodecrunch)        flags |= CHECKXFLAG_NODECRUNCH;
  421.             if(Args.nounlink)         flags |= CHECKXFLAG_NOUNLINK;
  422.             if(Args.nounarchive)    flags |= CHECKXFLAG_NOUNARCHIVE;
  423.             if(Args.nountrack)        flags |= CHECKXFLAG_NOUNTRACK;
  424.             if(Args.nostrip)        flags |= CHECKXFLAG_NOSTRIP;
  425.             if(Args.debug)        flags |= CHECKXFLAG_DEBUG;
  426.             if(Args.quiet)        flags |= CHECKXFLAG_QUIET;
  427.             if(Args.sectorcheck)    flags |= CHECKXFLAG_SECTORCHECK;
  428.  
  429.             if(!Args.novirus)
  430.             {
  431.               if((xvsbase = (XVSBASE *) OpenLibrary("xvs.library", 33)))
  432.               {
  433.                 flags |= CHECKXFLAG_XVSLIB;
  434.                 xvsBase = xvsbase;
  435.               }
  436.         }
  437.  
  438.             if(!Args.log || (log = Open(Args.log, MODE_READWRITE)))
  439.             {
  440.           APTR win;
  441.           ULONG s1 = 0, s2 = 0, msecs;
  442.           struct IntuitionBase *intuitionbase;
  443.  
  444.           win = task->pr_WindowPtr;
  445.           if(!Args.nosilent)
  446.             task->pr_WindowPtr = (APTR) -1;
  447.           /* prevent dos requests */
  448.  
  449.           if(log)
  450.           {
  451.             SetFileSize(log, 0, OFFSET_BEGINNING);
  452.             SetProtection(Args.log, FIBF_EXECUTE);
  453.             SetLogComment(fd, Args.log);
  454.           }
  455.  
  456.               fd->fd_Flags = flags | CHECKXFLAG_NAMEPRINTED; /* for possible warnings display */
  457.               fd->fd_LogFileFH = log;
  458.  
  459.           if(!xvsbase)
  460.             PrintCHKXErr(fd, CHKXWARN_NOVIRUS);
  461.           else
  462.           {
  463.                 struct xvsMemoryInfo *mi;
  464.  
  465.             if(!xvsSelfTest())
  466.               PrintCHKXErr(fd, CHKXWARN_XVSSELFTEST);
  467.  
  468.                   if((mi = (struct xvsMemoryInfo *) xvsAllocObject(XVSOBJ_MEMORYINFO)))
  469.                   { 
  470.                     if(xvsSurveyMemory(mi))
  471.                 PrintCHKXErr(fd, CHKXWARN_MEMVIRUS);
  472.  
  473.                     xvsFreeObject(mi);
  474.                   }
  475.           }
  476.  
  477.           if((intuitionbase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37)))
  478.           {
  479.             ASSIGN_INT
  480.             CurrentTime(&s1, &msecs);
  481.           }    
  482.  
  483.           ++fd->fd_RecurseDepth;
  484.           error = DoDirectoryScan(fd, Args.from, Args.save) ? RETURN_FAIL : RETURN_OK;
  485.           --fd->fd_RecurseDepth;
  486.           fd->fd_Flags |= CHECKXFLAG_NAMEPRINTED; /* for result texts */
  487.  
  488.           task->pr_WindowPtr = win;
  489.  
  490.           if(intuitionbase)
  491.           {
  492.             CurrentTime(&s2, &msecs);
  493.             s2 -= s1;
  494.             s1 = s2 / 60;
  495.             s2 %= 60;
  496.             msecs = s1 / 60;
  497.             s1 %= 60;
  498.             PrintCHKXTxt(fd, "\nTime needed for check: %2ld:%02ld:%02ld", msecs, s1, s2);
  499.             CloseLibrary((struct Library *) intuitionbase);
  500.           }
  501.           if(fd->fd_NumVirus && !fd->fd_NumSectors)
  502.             PrintCHKXTxt(fd, "The scan detected %ld virus%s.", fd->fd_NumVirus, fd->fd_NumVirus > 1 ? "es" : "");
  503.           else if(fd->fd_NumVirus && fd->fd_NumSectors)
  504.             PrintCHKXTxt(fd, "The scan detected %ld virus%s and %ld defective sector%s.", fd->fd_NumVirus,
  505.             fd->fd_NumVirus > 1 ? "es" : "", fd->fd_NumSectors, fd->fd_NumSectors > 1 ? "s" : "");
  506.           else if(!fd->fd_NumVirus && fd->fd_NumSectors)
  507.             PrintCHKXTxt(fd, "The scan detected %ld defective sector%s.", fd->fd_NumSectors,
  508.             fd->fd_NumSectors > 1 ? "s" : "");
  509.           if(fd->fd_CHKXErrors || fd->fd_XADErrors || fd->fd_XFDErrors || fd->fd_CorruptedArchives)
  510.             PrintCHKXTxt(fd, "There were errors (CheckX/XFD/XAD/corrupted archives): %ld/%ld/%ld/%ld.",
  511.             fd->fd_CHKXErrors, fd->fd_XFDErrors, fd->fd_XADErrors, fd->fd_CorruptedArchives);
  512.           if(!error && (fd->fd_NumVirus || !xvsBase))
  513.           {
  514.             SetIoErr(0);
  515.             error = RETURN_WARN;
  516.           }
  517.  
  518.               if(log)
  519.                 Close(log);
  520.             }
  521.             else if(Args.log && !Args.quiet)
  522.               Printf("Could not create logfile.\n");
  523.  
  524.         if(xvsbase)
  525.           CloseLibrary((struct Library *) xvsbase);
  526.             FreeArgs(rda);
  527.           }
  528.           else
  529.             PrintFault(IoErr(), "CheckX");
  530.           FreeDosObject(DOS_RDARGS, rda);
  531.         }
  532.     CloseLibrary((struct Library *) xadmasterbase);
  533.       }
  534.       else
  535.         Printf("CheckX requires xadmaster.library version 10 or better.\n");
  536.       CloseLibrary((struct Library *) xfdmasterbase);
  537.     }
  538.     else
  539.       Printf("CheckX requires xfdmaster.library version 39 or better.\n");
  540.     CloseLibrary((struct Library *) dosbase);
  541.   }
  542.  
  543.   return error;
  544. }
  545.  
  546. static void clear(STRPTR buf, ULONG size)
  547. {
  548.   while(size--)
  549.     *(buf++) = 0;
  550. }
  551.  
  552. static void SetLogComment(struct FileData *fd, STRPTR name)
  553. {
  554.   UBYTE com[30];
  555.   struct MsgPort *TimerMP;
  556.  
  557.   if((TimerMP = CreateMsgPort()))
  558.   {
  559.     struct timerequest *TimerIO;
  560.  
  561.     if((TimerIO = (struct timerequest *) CreateIORequest(TimerMP,
  562.     sizeof(struct timerequest))))
  563.     {
  564.       if(!OpenDevice("timer.device",UNIT_VBLANK,
  565.       (struct IORequest *)TimerIO,0))
  566.       {
  567.         struct UtilityBase *utilitybase;
  568.  
  569.         TimerIO->tr_node.io_Command = TR_GETSYSTIME;
  570.         DoIO((struct IORequest *) TimerIO);
  571.         if((utilitybase = (struct UtilityBase *) OpenLibrary("utility.library",37)))
  572.         {
  573.           struct ClockData dat;
  574.  
  575.       ASSIGN_UTIL
  576.           Amiga2Date(TimerIO->tr_time.tv_secs, &dat);
  577.           SPrintF(com, "CheckX " VERSION "." REVISION " - %02ld.%02ld.%ld", dat.mday, dat.month, dat.year);
  578.       CloseLibrary((struct Library *) utilitybase);
  579.       SetComment(name, com);
  580.         }
  581.         CloseDevice((struct IORequest *) TimerIO);
  582.       }
  583.       DeleteIORequest(TimerIO);
  584.     }
  585.     DeleteMsgPort(TimerMP);
  586.   }
  587. }
  588.  
  589. /* This scans a directory and calls DoFileOpen for every file. It
  590.    automatically creates SAVE destination directories when necessary. */
  591. static LONG DoDirectoryScan(struct FileData *fd, STRPTR name, STRPTR sav)
  592. {
  593.   struct AnchorPath *APath;
  594.   LONG error = CHKXERR_SCANERR;
  595.   ULONG retval;
  596.  
  597.   if(!(fd->fd_Flags & CHECKXFLAG_SAVE) || !sav ||
  598.   (fd->fd_SaveDirL = Lock(sav, SHARED_LOCK)))
  599.   {
  600.     if((APath = (struct AnchorPath *) AllocMem(sizeof(struct AnchorPath) +
  601.     512, MEMF_PUBLIC|MEMF_CLEAR)))
  602.     {
  603.       fd->fd_Name = APath->ap_Buf;
  604.       APath->ap_Strlen = 256;
  605.       for(retval = MatchFirst(name, APath); !retval;
  606.       retval = MatchNext(APath))
  607.       {
  608.         if(APath->ap_Flags & APF_DIDDIR)
  609.         {
  610.           OpenParentDir(fd);
  611.           APath->ap_Flags &= ~APF_DIDDIR; /* clear flag */
  612.         }
  613.         else if(APath->ap_Info.fib_DirEntryType > 0)
  614.         {
  615.           if(fd->fd_Flags & CHECKXFLAG_ALL)
  616.           {
  617.             OpenNewDir(fd, APath->ap_Info.fib_FileName);
  618.             APath->ap_Flags |= APF_DODIR;
  619.           }
  620.         }
  621.         else
  622.         {
  623.       fd->fd_Flags &= CHKXCALLFLAGS;
  624.           fd->fd_LinkNum = 0;
  625.           PrintCHKXErr(fd, DoFileOpen(fd));
  626.  
  627.           while(fd->fd_MemList)
  628.             FreeCrunchMemList(fd, fd->fd_MemList->cml_MemoryRegion);
  629.         }
  630.         if((fd->fd_Flags & CHECKXFLAG_SAVE) && !fd->fd_SaveDirL)
  631.         {
  632.           error = CHKXERR_OPENDIR; break;
  633.         }
  634.         if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
  635.         {
  636.           error = CHKXERR_BREAK; break;
  637.         }
  638.       }
  639.       MatchEnd(APath);
  640.  
  641.       if(retval == ERROR_NO_MORE_ENTRIES)
  642.         error = 0;
  643.  
  644.       FreeMem(APath, sizeof(struct AnchorPath) + 512);
  645.     }
  646.     else
  647.       error = CHKXERR_NOMEMORY;
  648.  
  649.     if(sav && fd->fd_SaveDirL)
  650.       UnLock(fd->fd_SaveDirL);
  651.   }
  652.   else
  653.     error = CHKXERR_OPENDIR;
  654.  
  655.   fd->fd_Flags |= CHECKXFLAG_NAMEPRINTED;
  656.   if(error)
  657.     PrintCHKXErr(fd, error);
  658.  
  659.   return error;
  660. }
  661.  
  662. /* Open a file and call DoGetVirus to scan */
  663. static LONG DoFileOpen(struct FileData *fd)
  664. {
  665.   struct FileInfoBlock *fib;
  666.   LONG ret = 0;
  667.  
  668.   if((fib = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, 0)))
  669.   {
  670.     ULONG fh;
  671.  
  672.     if((fh = Open(fd->fd_Name, MODE_OLDFILE)))
  673.     {
  674.       if(ExamineFH(fh, fib))
  675.       {
  676.         APTR mem;
  677.  
  678.     if(!fib->fib_Size)
  679.           ret = CHKXWARN_EMPTY;
  680.         else if((mem = AllocMem(fib->fib_Size, MEMF_ANY)))
  681.         {
  682.           if(Read(fh, mem, fib->fib_Size) != fib->fib_Size)
  683.             ret = CHKXERR_READ;
  684.  
  685.           if(!ret && !(ret = AddCrunchMemList(fd, mem, fib->fib_Size)))
  686.           {
  687.             if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  688.             ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && fib->fib_Size > 4
  689.             && *((ULONG *)mem) == HUNK_HEADER))
  690.               PrintCHKXFile(fd);
  691.             ret = DoGetVirus(fd, mem, fib->fib_Size);
  692.           }
  693.           else
  694.             FreeMem(mem, fib->fib_Size);
  695.         }
  696.         else
  697.         {
  698.           fd->fd_ArcFileFH = fh;
  699.           PrintCHKXErr(fd, CHKXERR_NOMEMORYARC);
  700.       ret = DoFileUnArchive(fd, 0, fib->fib_Size);
  701.         }
  702.       }
  703.       else
  704.         ret = CHKXERR_EXAMINEERR;
  705.       Close(fh);
  706.     }
  707.     else
  708.       ret = CHKXERR_OPENERR;
  709.   
  710.     FreeDosObject(DOS_FIB, fib);
  711.   }
  712.   else
  713.     ret = CHKXERR_NOMEMORY;
  714.  
  715.   return ret;
  716. }
  717.  
  718. /* for SAVE option: open parent directory and try to delete the directory
  719.    we leave. This only succeeds, when the directory is empty. */
  720. static ULONG OpenParentDir(struct FileData *fd)
  721. {
  722.   ULONG g;
  723.   UBYTE name[300];
  724.  
  725.   if((g = fd->fd_SaveDirL))
  726.   {
  727.     NameFromLock(g, name, 300);
  728.     fd->fd_SaveDirL = ParentDir(g);
  729.     UnLock(g);
  730.  
  731.     DeleteFile(name);
  732.   }
  733.   else
  734.     return -1;
  735.  
  736.   return fd->fd_SaveDirL;
  737. }
  738.  
  739. /* Open a new subdirectory for SAVE option */
  740. static ULONG OpenNewDir(struct FileData *fd, STRPTR name)
  741. {
  742.   ULONG g;
  743.  
  744.   if(fd->fd_SaveDirL)
  745.   {
  746.     g = CurrentDir(fd->fd_SaveDirL);
  747.     if(!(fd->fd_SaveDirL = Lock(name, SHARED_LOCK)))
  748.       if((fd->fd_SaveDirL = CreateDir(name)))
  749.         ChangeMode(CHANGE_LOCK, fd->fd_SaveDirL, SHARED_LOCK);
  750.     UnLock(CurrentDir(g));
  751.   }
  752.   else
  753.     return -1;
  754.  
  755.   return fd->fd_SaveDirL;
  756. }
  757.  
  758. static LONG DoGetVirus(struct FileData *fd, APTR buffer, ULONG buflength)
  759. {
  760.   if(!buflength) /* restarted with empty file */
  761.     return CHKXWARN_EMPTY;
  762.  
  763.   if(fd->fd_Flags & CHECKXFLAG_XVSLIB)
  764.   {
  765.     APTR mem;
  766.       
  767.     if((mem = AllocMem(buflength, MEMF_ANY)))
  768.     {
  769.       struct xvsFileInfo *fi;
  770.       if((fi = (struct xvsFileInfo *) xvsAllocObject(XVSOBJ_FILEINFO)))
  771.       {
  772.         ULONG i;
  773.     /* xvs may modify the buffer! */
  774.         CopyMem(buffer, mem, buflength);
  775.         fi->xvsfi_File = mem;
  776.         fi->xvsfi_FileLen = buflength;
  777.         i = xvsCheckFile(fi);
  778.         if(i == XVSFT_DATAVIRUS)
  779.         {
  780.        PrintCHKXTxt(fd, "Data-Virus '%s'", fi->xvsfi_Name);
  781.        ++fd->fd_NumVirus;
  782.      }
  783.         else if(i == XVSFT_FILEVIRUS)
  784.         {
  785.        PrintCHKXTxt(fd, "File-Virus '%s'", fi->xvsfi_Name);
  786.        ++fd->fd_NumVirus;
  787.      }
  788.         else if(i == XVSFT_LINKVIRUS)
  789.         {
  790.        PrintCHKXTxt(fd, "Link-Virus '%s'", fi->xvsfi_Name);
  791.        ++fd->fd_NumVirus;
  792.      }
  793.         xvsFreeObject(fi);
  794.       }
  795.       else
  796.         PrintCHKXErr(fd, CHKXERR_NOVIRUS);
  797.       FreeMem(mem, buflength);
  798.     }
  799.     else
  800.       PrintCHKXErr(fd, CHKXERR_NOVIRUS);
  801.  
  802.     if(fd->fd_Flags & CHECKXFLAG_SECTORCHECK) /* && !(buflength & 0x1FF)*/
  803.       PrintCHKXErr(fd, DoSectorCheck(fd, buffer, buflength, 0));
  804.   }
  805.  
  806.   return DoFileUnArchive(fd, buffer, buflength);
  807. }
  808.  
  809. static LONG DoSectorCheck(struct FileData *fd, APTR buffer, ULONG buflength, ULONG pos)
  810. {
  811.   LONG err = 0;
  812.   struct xvsSectorInfo *si;
  813.  
  814.   if((si = (struct xvsSectorInfo *) xvsAllocObject(XVSOBJ_SECTORINFO)))
  815.   {
  816.     ULONG i, j, k;
  817.  
  818.     j = buflength >> 9; /* divide by 512 */
  819.     for(i = 0; i < j; ++i)
  820.     {
  821.       si->xvssi_Sector = ((STRPTR) buffer) + (i<<9);
  822.       si->xvssi_Key = pos + i;
  823.       k = xvsCheckSector(si);
  824.       if(k == XVSST_DESTROYED)
  825.       {
  826.         ++fd->fd_NumSectors;
  827.         PrintCHKXTxt(fd, "Sector %ld destroyed by '%s'", si->xvssi_Key, si->xvssi_Name);
  828.       }
  829.       else if(k == XVSST_INFECTED)
  830.       {
  831.         ++fd->fd_NumSectors;
  832.         PrintCHKXTxt(fd, "Sector %ld infected by '%s'", si->xvssi_Key, si->xvssi_Name);
  833.       }
  834.     }
  835.     xvsFreeObject(si);
  836.   }
  837.   else
  838.     err = CHKXERR_NOSECTOR;
  839.  
  840.   return err;
  841. }
  842.  
  843. /* For bootblocks */
  844. static ASM(LONG) OutHookCheckX(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp))
  845. {
  846.   LONG i, j;
  847.   STRPTR s;
  848.   struct FileData *fd;
  849.  
  850.   fd = (struct FileData *) hook->h_Data;
  851.   switch(hp->xhp_Command)
  852.   {
  853.   case XADHC_WRITE:
  854.     j = hp->xhp_BufferSize;
  855.     s = hp->xhp_BufferPtr;
  856.     if((i = 1024-hp->xhp_DataPos) > j)
  857.       i = j;
  858.     if(i > 0)
  859.     {
  860.       CopyMem(s, fd->fd_Memory+hp->xhp_DataPos, i);
  861.       hp->xhp_DataPos += i;
  862.       j -= i;
  863.       s += i;
  864.     }
  865.     if(hp->xhp_DataPos == 1024 && i > 0)
  866.     {
  867.       if(((fd->fd_Flags & CHECKXFLAG_XVSLIB) && (fd->fd_Flags & CHECKXFLAG_SECTORCHECK)))
  868.       {
  869.         i = DoSectorCheck(fd, fd->fd_Memory, 1024, 0);
  870.         if(i)
  871.         {
  872.           PrintCHKXErr(fd, i);
  873.           return 10000;
  874.         }
  875.       }
  876.       else
  877.         return 10000;
  878.     }
  879.     if(hp->xhp_DataPos >= 1024)
  880.     {
  881.       LONG k;
  882.       while(j)
  883.       {
  884.         k = hp->xhp_DataPos & 0x1FF;
  885.         if((i = 512-k) > j)
  886.           i = j;
  887.         CopyMem(s, fd->fd_Memory+1024+k, i);
  888.         hp->xhp_DataPos += i;
  889.         j -= i;
  890.         s += i;
  891.         if(!(hp->xhp_DataPos & 0x1FF))
  892.         {
  893.           i = DoSectorCheck(fd, fd->fd_Memory+1024, 512, (hp->xhp_DataPos>>9)-1);
  894.           if(i)
  895.           {
  896.             PrintCHKXErr(fd, i);
  897.             return 10000;
  898.           }
  899.         }
  900.       }
  901.     }
  902.   case XADHC_INIT:
  903.   case XADHC_FREE:
  904.   case XADHC_ABORT:
  905.     break;
  906.   default: return XADERR_NOTSUPPORTED;
  907.   }
  908.  
  909.   return 0;
  910. }
  911.  
  912. /* To get real file size */
  913. static ASM(LONG) OutHookCheckXSize(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp))
  914. {
  915.   switch(hp->xhp_Command)
  916.   {
  917.   case XADHC_WRITE:
  918.     hp->xhp_DataPos += hp->xhp_BufferSize;
  919.     hook->h_Data = (APTR) hp->xhp_DataPos;
  920.   case XADHC_INIT:
  921.   case XADHC_FREE:
  922.   case XADHC_ABORT:
  923.     break;
  924.   default: return XADERR_NOTSUPPORTED;
  925.   }
  926.  
  927.   return 0;
  928. }
  929.  
  930. static void CheckArcFiles(struct FileData *fd, struct xadArchiveInfo *ai, ULONG disk)
  931. {
  932.   struct xadFileInfo *fi;
  933.   struct TagItem ti[6];
  934.   LONG numloop = 0;
  935.   STRPTR buf = 0;
  936.  
  937.   ti[0].ti_Tag = XAD_OUTMEMORY;
  938.   ti[1].ti_Tag = XAD_OUTSIZE;
  939.   ti[2].ti_Tag = XAD_ENTRYNUMBER;
  940.   ti[3].ti_Tag = XAD_PROGRESSHOOK;
  941.   ti[3].ti_Data = (ULONG) &breakhook;
  942.   ti[4].ti_Tag = TAG_DONE;
  943.  
  944.   fi = ai->xai_FileInfo;
  945.   while(!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) && fi)
  946.   {
  947.     APTR dest;
  948.  
  949.     if(!(fi->xfi_Flags & (XADFIF_DIRECTORY|XADFIF_LINK)))
  950.     {
  951.       LONG err = 0;
  952.       ULONG flags, size, linknum;
  953.  
  954.       flags = fd->fd_Flags;
  955.       linknum = fd->fd_LinkNum;
  956.       fd->fd_LinkNum = 0;
  957.       if(!(fi->xfi_Flags & XADFIF_NOFILENAME))
  958.         fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  959.       fd->fd_Name = fi->xfi_FileName;
  960.       fd->fd_RecurseDepth++;
  961.       fd->fd_ArchiveDepth++;
  962.  
  963.       size = fi->xfi_Size;
  964.       if(fi->xfi_Flags & XADFIF_NOUNCRUNCHSIZE)
  965.       {
  966.     struct Hook hook = {{0,0},(ULONG (*)()) OutHookCheckXSize, 0, 0};
  967.       struct TagItem ti[5];
  968.  
  969.     ti[0].ti_Tag = XAD_OUTHOOK;
  970.     ti[0].ti_Data = (ULONG) &hook;
  971.       ti[1].ti_Tag = XAD_ENTRYNUMBER;
  972.     ti[1].ti_Data = fi->xfi_EntryNumber;
  973.         ti[2].ti_Tag = XAD_PROGRESSHOOK;
  974.         ti[2].ti_Data = (ULONG) &breakhook;
  975.       ti[3].ti_Tag = buf ? XAD_PASSWORD : TAG_IGNORE;
  976.       ti[3].ti_Data = (ULONG) buf;
  977.         ti[4].ti_Tag = TAG_DONE;
  978.  
  979.         if(!(err = (disk ? xadDiskFileUnArcA(ai, ti) : xadFileUnArcA(ai, ti))))
  980.       size = (ULONG) hook.h_Data;
  981.       }
  982.  
  983.       if(!size)
  984.       {
  985.         if(!err)
  986.           err = CHKXWARN_EMPTY;
  987.       }
  988.       else if((dest = AllocMem(size, MEMF_PUBLIC)))
  989.       {
  990.         if(!(err = AddCrunchMemList(fd, dest, size)))
  991.         {
  992.       ti[0].ti_Data = (ULONG) dest;
  993.       ti[1].ti_Data = size;
  994.       ti[2].ti_Data = fi->xfi_EntryNumber;
  995.           ti[4].ti_Tag = buf ? XAD_PASSWORD : TAG_IGNORE;
  996.       ti[4].ti_Data = (ULONG) buf;
  997.  
  998.           if((err = disk ? xadDiskFileUnArcA(ai, ti) : xadFileUnArcA(ai, ti)))
  999.       {
  1000.         if(disk)
  1001.           err = CHKXERR_READ;
  1002.         else
  1003.               err += XADERR_OFFSET;
  1004.           }
  1005.           else
  1006.           {
  1007.             if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  1008.         ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && size > 4
  1009.         && *((ULONG *)dest) == HUNK_HEADER))
  1010.               PrintCHKXFile(fd);
  1011.             err = DoGetVirus(fd, dest, size);
  1012.           }
  1013.           FreeCrunchMemList(fd, dest);
  1014.         }
  1015.         else
  1016.           FreeMem(dest, size);
  1017.       }
  1018.       else
  1019.         err = CHKXERR_NOMEMORY;
  1020.  
  1021.       if((err == XADERR_OFFSET + XADERR_PASSWORD) && !numloop)
  1022.       {
  1023.         struct Library *xpkbase;
  1024.         if(fd->fd_Flags & CHECKXFLAG_ASKPWD && (xpkbase = OpenLibrary(XPKNAME, 4)))
  1025.         {
  1026.           if(buf)
  1027.             FreeMem(buf, 256);
  1028.           if((buf = (STRPTR) AllocMem(256, MEMF_ANY|MEMF_CLEAR)))
  1029.       {
  1030.             if(XpkPassRequestTags(XPK_PasswordBuf, buf, XPK_PassBufSize, 256,
  1031.             XPK_PassTitle, fd->fd_Name, TAG_DONE))
  1032.             {
  1033.               FreeMem(buf, 256); buf = 0; fi = fi->xfi_Next;
  1034.             }
  1035.             else
  1036.             {
  1037.               numloop = -1; err = 0;
  1038.             }
  1039.           }
  1040.  
  1041.           CloseLibrary(xpkbase);
  1042.         }
  1043.       }
  1044.  
  1045.       if(err)
  1046.       {
  1047.         if(fi->xfi_Flags & XADFIF_NOFILENAME)
  1048.           fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  1049.         PrintCHKXErr(fd, err);
  1050.       }
  1051.  
  1052.       --fd->fd_RecurseDepth;
  1053.       --fd->fd_ArchiveDepth;
  1054.       fd->fd_Flags = flags;
  1055.       fd->fd_LinkNum = linknum;
  1056.     }
  1057.     if(++numloop)
  1058.     {
  1059.       fi = fi->xfi_Next;
  1060.       numloop = 0;
  1061.     }
  1062.     else
  1063.       ++numloop;
  1064.   }
  1065.   if(buf)
  1066.     FreeMem(buf, 256);
  1067. }
  1068.  
  1069. /* Tests if a file is an archive. When yes, the archive is unarchived and
  1070.    the result files are scanned. Else DoFileUnLink is called.
  1071. */
  1072. static LONG DoFileUnArchive(struct FileData *fd, APTR buffer, ULONG buflength)
  1073. {
  1074.   LONG err = 0;
  1075.   struct TagItem tih[2];
  1076.   struct xadArchiveInfo *ai;
  1077.  
  1078.   if(buffer) { tih[0].ti_Tag = XAD_INMEMORY; tih[0].ti_Data = (ULONG) buffer; }
  1079.   else { tih[0].ti_Tag = XAD_INFILEHANDLE; tih[0].ti_Data = (ULONG) fd->fd_ArcFileFH; }
  1080.   tih[1].ti_Tag = TAG_DONE;
  1081.  
  1082.   if((ai = (struct xadArchiveInfo *) xadAllocObject(XADOBJ_ARCHIVEINFO, 0)))
  1083.   {
  1084.     STRPTR arcname;
  1085.  
  1086.     arcname = fd->fd_Name;
  1087.     if(!xadGetInfo(ai, XAD_INSIZE, buflength, XAD_PROGRESSHOOK, &breakhook, TAG_MORE, tih))
  1088.     {
  1089.       if(ai->xai_Flags & XADAIF_FILECORRUPT)
  1090.       {
  1091.         ++fd->fd_CorruptedArchives;
  1092.         PrintCHKXTxt(fd, "%s (ARCHIVE, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  1093.       }
  1094.       else
  1095.         PrintCHKXTxt(fd, "%s (ARCHIVE)", ai->xai_Client->xc_ArchiverName);
  1096.         
  1097.       if(!(!(fd->fd_Flags & CHECKXFLAG_NOUNARCHIVE) && ai->xai_FileInfo ||
  1098.       !(fd->fd_Flags & CHECKXFLAG_NOUNTRACK) && ai->xai_DiskInfo))
  1099.       {
  1100.         if(buffer)
  1101.           err = DoFileUnLink(fd, buffer, buflength);
  1102.       }
  1103.       else if(!OpenNewDir(fd, FilePart(fd->fd_Name)))
  1104.         err = CHKXERR_OPENERR;
  1105.       else
  1106.       {
  1107.     if(!(fd->fd_Flags & CHECKXFLAG_NOUNARCHIVE) && ai->xai_FileInfo)
  1108.       CheckArcFiles(fd, ai, 0);
  1109.     if(!(fd->fd_Flags & CHECKXFLAG_NOUNTRACK) && ai->xai_DiskInfo)
  1110.     {
  1111.       STRPTR buf;
  1112.  
  1113.       /* reduce stacksize, as we have a recursive program */
  1114.       if((buf = (STRPTR) AllocMem(1024+512, MEMF_PUBLIC)))
  1115.       {
  1116.             ULONG flags;
  1117.         struct xadDiskInfo *di;
  1118.         struct xadArchiveInfo *aid;
  1119.         di = ai->xai_DiskInfo;
  1120.  
  1121.             flags = fd->fd_Flags;
  1122.         while(!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) && di)
  1123.         {
  1124.           if(di->xdi_SectorSize != 512 || (di->xdi_TrackSectors != 11 &&
  1125.           di->xdi_TrackSectors != 22) || (!(di->xdi_Flags & XADDIF_NOHEADS) &&
  1126.           di->xdi_Heads != 2) || (!(di->xdi_Flags & XADDIF_NOCYLINDERS) &&
  1127.           di->xdi_Cylinders != 80))
  1128.             SPrintF(buf, "-disk image %ld", di->xdi_EntryNumber);
  1129.           else
  1130.           {
  1131.             if(!(di->xdi_Flags & (XADDIF_NOHIGHCYL|XADDIF_NOLOWCYL)) &&
  1132.          (di->xdi_LowCyl || di->xdi_HighCyl != 79))
  1133.                   SPrintF(buf, "-disk image %ld (%s, %ld to %ld)",
  1134.                   di->xdi_EntryNumber, di->xdi_TrackSectors == 22 ?
  1135.                   "HD" : "DD", di->xdi_LowCyl, di->xdi_HighCyl);
  1136.                 else
  1137.                   SPrintF(buf, "-disk image %ld (%s)",
  1138.                   di->xdi_EntryNumber, di->xdi_TrackSectors == 22 ? "HD" : "DD");
  1139.               }
  1140.       
  1141.               fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  1142.           fd->fd_Name = buf;
  1143.               PrintCHKXFile(fd);
  1144.  
  1145.           if(di->xdi_TextInfo)
  1146.           {
  1147.                 struct xadTextInfo *ti;
  1148.                 ULONG flags, i = 1, linknum;
  1149.  
  1150.             for(ti = di->xdi_TextInfo; ti; ti = ti->xti_Next)
  1151.             {
  1152.               if(ti->xti_Size && ti->xti_Text)
  1153.               {
  1154.                     flags = fd->fd_Flags;
  1155.                     fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  1156.                     fd->fd_Flags |= CHECKXFLAG_NOFREEMEM;
  1157.                     fd->fd_RecurseDepth++;
  1158.                     linknum = fd->fd_LinkNum;
  1159.                     fd->fd_LinkNum = 0;
  1160.                     SPrintF(buf, "--infotext %ld (size %ld)", i, ti->xti_Size);
  1161.                     if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  1162.                     ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && ti->xti_Size > 4
  1163.                     && *((ULONG *)(ti->xti_Text)) == HUNK_HEADER))
  1164.                       PrintCHKXFile(fd);
  1165.                     PrintCHKXErr(fd, DoGetVirus(fd, ti->xti_Text, ti->xti_Size));
  1166.                     --fd->fd_RecurseDepth;
  1167.                     fd->fd_Flags = flags;
  1168.                     fd->fd_LinkNum = linknum;
  1169.                   }
  1170.                   ++i;
  1171.                 }
  1172.               }
  1173.  
  1174.           /* check bootblock here - special outhook for bootblock extraction */
  1175.           if(!di->xdi_LowCyl) /* boot check only, when first block! */
  1176.           {
  1177.             struct Hook hook = {{0,0},(ULONG (*)()) OutHookCheckX, 0};
  1178.             LONG i;
  1179.  
  1180.         fd->fd_Memory = buf;
  1181.             hook.h_Data = fd;
  1182.             i = xadDiskUnArc(ai, XAD_ENTRYNUMBER, di->xdi_EntryNumber,
  1183.             XAD_OUTHOOK, &hook, XAD_PROGRESSHOOK, &breakhook, TAG_DONE);
  1184.             if(!i || i == 10000) /* special hook return code! */
  1185.             {
  1186.               struct xvsBootInfo *bi;
  1187.  
  1188.               if((bi = (struct xvsBootInfo *) xvsAllocObject(XVSOBJ_BOOTINFO)))
  1189.                 {
  1190.                 bi->xvsbi_Bootblock = buf;
  1191.                 if(xvsCheckBootblock(bi) == XVSBT_VIRUS)
  1192.                 {
  1193.                   PrintCHKXTxt(fd, "Boot-Virus '%s'", bi->xvsbi_Name);
  1194.                   ++fd->fd_NumVirus;
  1195.                 }
  1196.                 xvsFreeObject(bi);
  1197.                 }
  1198.                 else
  1199.                   PrintCHKXErr(fd, CHKXERR_NOBOOTVIRUS);
  1200.               }
  1201.               else
  1202.                 PrintCHKXErr(fd, CHKXERR_NOBOOTVIRUS);
  1203.           }
  1204.     
  1205.           if((aid = (struct xadArchiveInfo *) xadAllocObject(XADOBJ_ARCHIVEINFO, 0)))
  1206.           {
  1207.             struct TagItem ti[4];
  1208.             struct xadClient *cl;
  1209.  
  1210.           ti[0].ti_Tag = XAD_INSIZE;
  1211.           ti[0].ti_Data = buflength;
  1212.           ti[1].ti_Tag = XAD_ENTRYNUMBER;
  1213.           ti[1].ti_Data = di->xdi_EntryNumber;
  1214.                 ti[2].ti_Tag = XAD_PROGRESSHOOK;
  1215.                 ti[2].ti_Data = (ULONG) &breakhook;
  1216.           ti[3].ti_Tag = TAG_MORE;
  1217.           ti[3].ti_Data = (ULONG) tih;
  1218.     
  1219.           if(!(err = xadGetDiskInfo(aid, XAD_NOEMPTYERROR, 1, XAD_PROGRESSHOOK, &breakhook,
  1220.           XAD_INDISKARCHIVE, ti, TAG_DONE)))
  1221.           {
  1222.             if(aid->xai_Flags & XADAIF_FILECORRUPT)
  1223.             {
  1224.                     ++fd->fd_CorruptedArchives;
  1225.                     PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", aid->xai_Client->xc_ArchiverName);
  1226.             }
  1227.                   else
  1228.                     PrintCHKXTxt(fd, "%s (FILESYSTEM)", aid->xai_Client->xc_ArchiverName);
  1229.           CheckArcFiles(fd, aid, 1);
  1230.           cl = aid->xai_Client->xc_Next;
  1231.           xadFreeInfo(aid);
  1232.           while(cl) /* multiple filesystems */
  1233.           {
  1234.               if(!xadGetDiskInfo(aid, XAD_NOEMPTYERROR, 1, XAD_INDISKARCHIVE, ti,
  1235.               XAD_PROGRESSHOOK, &breakhook, XAD_STARTCLIENT, cl, TAG_MORE, TAG_DONE))
  1236.               {
  1237.                 if(aid->xai_Flags & XADAIF_FILECORRUPT)
  1238.                 {
  1239.                         ++fd->fd_CorruptedArchives;
  1240.                         PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", aid->xai_Client->xc_ArchiverName);
  1241.                 }
  1242.                       else
  1243.                         PrintCHKXTxt(fd, "%s (FILESYSTEM)", aid->xai_Client->xc_ArchiverName);
  1244.               CheckArcFiles(fd, aid, 1);
  1245.               cl = aid->xai_Client->xc_Next;
  1246.               xadFreeInfo(aid);
  1247.             }
  1248.             else
  1249.               cl = 0;
  1250.           }
  1251.           }
  1252.           else
  1253.           {
  1254.             if(err != XADERR_FILESYSTEM)
  1255.               PrintCHKXErr(fd, XADERR_OFFSET + err);
  1256.             err = 0;
  1257.           }
  1258.           xadFreeObjectA(aid, 0);
  1259.           }
  1260.  
  1261.               di = di->xdi_Next;
  1262.             } /* while */
  1263.             fd->fd_Flags = flags;
  1264.             FreeMem(buf, 1024+512);
  1265.           } /* AllocMem buffer */
  1266.           else
  1267.             err = CHKXERR_NOMEMORY;
  1268.         } /* is there a disk entry? */
  1269.         if(!OpenParentDir(fd))
  1270.           err = CHKXERR_OPENDIR;
  1271.       }
  1272.  
  1273.       xadFreeInfo(ai);
  1274.     } /* xadGetInfo */
  1275.     else if(!xadGetDiskInfo(ai, XAD_NOEMPTYERROR, 1, XAD_PROGRESSHOOK, &breakhook,
  1276.     XAD_INSIZE, buflength, TAG_MORE, tih))
  1277.     {
  1278.       struct xadClient *cl;
  1279.       ULONG isvalid = 1;
  1280.  
  1281.       if(ai->xai_Flags & XADAIF_FILECORRUPT)
  1282.       {
  1283.         ++fd->fd_CorruptedArchives;
  1284.         PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  1285.       }
  1286.       else
  1287.         PrintCHKXTxt(fd, "%s (FILESYSTEM)", ai->xai_Client->xc_ArchiverName);
  1288.  
  1289.       if(fd->fd_Flags & CHECKXFLAG_NOUNTRACK)
  1290.       {
  1291.         if(buffer)
  1292.           err = DoFileUnLink(fd, buffer, buflength);
  1293.       }
  1294.       else if(!OpenNewDir(fd, FilePart(fd->fd_Name)))
  1295.         err = CHKXERR_OPENERR;
  1296.       else
  1297.       {
  1298.         CheckArcFiles(fd, ai, 1);
  1299.         while(isvalid && (cl = ai->xai_Client->xc_Next))
  1300.         {
  1301.           xadFreeInfo(ai);
  1302.           if(!xadGetDiskInfo(ai, XAD_NOEMPTYERROR, 1, XAD_PROGRESSHOOK, &breakhook,
  1303.           XAD_INSIZE, buflength, XAD_STARTCLIENT, cl, TAG_MORE, tih))
  1304.           {
  1305.             if(ai->xai_Flags & XADAIF_FILECORRUPT)
  1306.             {
  1307.               ++fd->fd_CorruptedArchives;
  1308.               PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  1309.             }
  1310.             else
  1311.               PrintCHKXTxt(fd, "%s (FILESYSTEM)", ai->xai_Client->xc_ArchiverName);
  1312.             CheckArcFiles(fd, ai, 1);
  1313.           }
  1314.           else
  1315.             isvalid = 0;
  1316.         } 
  1317.         if(!OpenParentDir(fd))
  1318.           err = CHKXERR_OPENDIR;
  1319.       }
  1320.       if(isvalid)
  1321.         xadFreeInfo(ai);
  1322.     }
  1323.     else if(buffer)
  1324.       err = DoFileUnLink(fd, buffer, buflength);
  1325.  
  1326.     fd->fd_Name = arcname;
  1327.     xadFreeObjectA(ai, 0);
  1328.   }
  1329.   else
  1330.     err = CHKXERR_NOMEMORY;
  1331.  
  1332.   return err;
  1333. }
  1334.  
  1335. /* Tries to unlink a file. When the file was linked, we call DoGetVirus
  1336.    for the two parts to check if they may be archives, else we call
  1337.    DoFileUnCrunch.
  1338. */
  1339. static LONG DoFileUnLink(struct FileData *fd, APTR buffer, ULONG buflength)
  1340. {
  1341.   LONG ret = CHKXERR_NOMEMORY;
  1342.   struct xfdLinkerInfo *xli;
  1343.  
  1344.   if((xli = (struct xfdLinkerInfo *) xfdAllocObject(XFDOBJ_LINKERINFO)))
  1345.   {
  1346.     xli->xfdli_Buffer = buffer;
  1347.     xli->xfdli_BufLen = buflength;
  1348.     if(xfdRecogLinker(xli))
  1349.     {
  1350.       PrintCHKXTxt(fd, xli->xfdli_LinkerName);
  1351.       if(fd->fd_Flags & CHECKXFLAG_NOUNLINK)
  1352.         ret = DoFileUnCrunch(fd, buffer, buflength);
  1353.       else if(xfdUnlink(xli))
  1354.       {
  1355.         ULONG flags;
  1356.     fd->fd_Flags |= CHECKXFLAG_LINKED;
  1357.         ++fd->fd_RecurseDepth;
  1358.         ++fd->fd_LinkNum;
  1359.     flags = fd->fd_Flags;
  1360.     fd->fd_Flags |= CHECKXFLAG_NOFREEMEM;
  1361.         PrintCHKXErr(fd, DoGetVirus(fd, xli->xfdli_Save1,
  1362.         xli->xfdli_SaveLen1));
  1363.         fd->fd_Flags = flags; /* CHECKXFLAG_NOFREEMEM is cleared */
  1364.         PrintCHKXErr(fd, DoGetVirus(fd, xli->xfdli_Save2,
  1365.         xli->xfdli_SaveLen2));
  1366.         ret = 0;
  1367.         --fd->fd_RecurseDepth;
  1368.       }
  1369.       else
  1370.         ret = XFDERR_OFFSET + xli->xfdli_Error;
  1371.     }
  1372.     else
  1373.       ret = DoFileUnCrunch(fd, buffer, buflength);
  1374.  
  1375.     xfdFreeObject(xli);
  1376.   }
  1377.   return ret;
  1378. }
  1379.  
  1380. /* Tries to decrunch a file. When it is crunched, we decrunch it and call
  1381.    DoGetVirus to start the loop again. Else we call unstripping.
  1382. */
  1383. static LONG DoFileUnCrunch(struct FileData *fd, APTR buffer, ULONG buflength)
  1384. {
  1385.   LONG ret = CHKXERR_NOMEMORY;
  1386.   struct xfdBufferInfo *xbi;
  1387.  
  1388.   if((xbi = (struct xfdBufferInfo *) xfdAllocObject(XFDOBJ_BUFFERINFO)))
  1389.   {
  1390.     xbi->xfdbi_SourceBuffer = buffer;
  1391.     xbi->xfdbi_SourceBufLen = buflength;
  1392.     xbi->xfdbi_Flags = XFDFF_RECOGEXTERN;
  1393.     if(xfdRecogBuffer(xbi))
  1394.     {
  1395.       struct Library *xpkbase;
  1396.       STRPTR buf = 0;
  1397.       ULONG buflen = 0;
  1398.  
  1399.       PrintCHKXTxt(fd, xbi->xfdbi_PackerFlags & XFDPFF_ADDR ? "%s (ADDRESS)" :
  1400.       "%s", xbi->xfdbi_PackerName);
  1401.  
  1402.       if(fd->fd_Flags & CHECKXFLAG_ASKPWD && (xpkbase =
  1403.       OpenLibrary(XPKNAME, 4)))
  1404.       {
  1405.     ASSIGN_XPK
  1406.         if(xbi->xfdbi_PackerFlags & XFDPFF_PASSWORD)
  1407.         {
  1408.           buflen = (xbi->xfdbi_MaxSpecialLen == 0xFFFF) ? 256 :
  1409.         xbi->xfdbi_MaxSpecialLen;
  1410.           if((buf = (STRPTR) AllocMem(buflen, MEMF_ANY|MEMF_CLEAR)))
  1411.       {
  1412.             if(!XpkPassRequestTags(XPK_PasswordBuf, buf, XPK_PassTitle, fd->fd_Name, 
  1413.             XPK_PassBufSize, buflen, TAG_DONE))
  1414.           xbi->xfdbi_Special = buf;
  1415.       }
  1416.         }
  1417.         else if(xbi->xfdbi_PackerFlags & XFDPFF_KEY16)
  1418.         {
  1419.       if(!XpkPassRequestTags(XPK_Key16BitPtr, &buflen, XPK_PassTitle, fd->fd_Name, TAG_DONE))
  1420.         xbi->xfdbi_Special = &buflen;
  1421.         }
  1422.         else if(xbi->xfdbi_PackerFlags & XFDPFF_KEY32)
  1423.         {
  1424.       if(!XpkPassRequestTags(XPK_Key32BitPtr, &buflen, XPK_PassTitle, fd->fd_Name, TAG_DONE))
  1425.             xbi->xfdbi_Special = &buflen;
  1426.         }
  1427.     CloseLibrary(xpkbase);
  1428.       }
  1429.       if(fd->fd_Flags & CHECKXFLAG_NODECRUNCH)
  1430.         ret = DoFileStrip(fd, buffer, buflength);
  1431.       else if(xfdDecrunchBuffer(xbi))
  1432.       {
  1433.         if((xbi->xfdbi_PackerFlags & XFDPFF_ADDR) &&
  1434.         !(fd->fd_Flags & CHECKXFLAG_ADDRESS))
  1435.         {
  1436.       fd->fd_Flags |= CHECKXFLAG_ADDRESS;
  1437.           PrintCHKXErr(fd, SaveUncrFile(fd, buffer, buflength));
  1438.         }
  1439.     fd->fd_Flags |= CHECKXFLAG_CRUNCHED;
  1440.         FreeCrunchMemList(fd, buffer);
  1441.         ++fd->fd_RecurseDepth;
  1442.         if(!(ret = AddCrunchMemList(fd, xbi->xfdbi_TargetBuffer,
  1443.         xbi->xfdbi_TargetBufLen)))
  1444.         {
  1445.           PrintCHKXErr(fd, DoGetVirus(fd, xbi->xfdbi_TargetBuffer,
  1446.             xbi->xfdbi_TargetBufSaveLen));
  1447.         }
  1448.         else
  1449.           FreeMem(xbi->xfdbi_TargetBuffer, xbi->xfdbi_TargetBufLen);
  1450.         --fd->fd_RecurseDepth;
  1451.       }
  1452.       else
  1453.         ret = XFDERR_OFFSET + xbi->xfdbi_Error;
  1454.  
  1455.       if(buf)
  1456.         FreeMem(buf, buflen);
  1457.     }
  1458.     else
  1459.       ret = DoFileStrip(fd, buffer, buflength);
  1460.  
  1461.     xfdFreeObject(xbi);
  1462.   }
  1463.   return ret;
  1464. }
  1465.  
  1466. /* Tries to strip useless hunks in a file. When there are some, we remove
  1467.    them and call DoGetVirus to start the loop again. Else we finish.
  1468.    When either unlinking or uncrunching happend in before loops, we may
  1469.    save the file when there was SAVE option.
  1470. */
  1471. static LONG DoFileStrip(struct FileData *fd, APTR buffer, ULONG buflength)
  1472. {
  1473.   LONG ret = 0;
  1474.   ULONG reslength = buflength;
  1475.  
  1476.   if(*((ULONG *) buffer) == 0x000003F3 && !(fd->fd_Flags &
  1477.   CHECKXFLAG_NOSTRIP))
  1478.     xfdStripHunks(buffer, buflength, &reslength,
  1479.     XFDSHF_NAME|XFDSHF_SYMBOL|XFDSHF_DEBUG);
  1480.     /* errors are not interpreted */
  1481.  
  1482.   if(buflength > reslength)
  1483.   {
  1484.     fd->fd_Flags |= CHECKXFLAG_STRIPPED;
  1485.     PrintCHKXTxt(fd, "%ld bytes stripped", buflength-reslength);
  1486.     ret = DoGetVirus(fd, buffer, reslength);
  1487.   }
  1488.   else if(!(fd->fd_Flags & CHECKXFLAG_ADDRESS))
  1489.     ret = SaveUncrFile(fd, buffer, buflength);
  1490.  
  1491.   FreeCrunchMemList(fd, buffer);
  1492.  
  1493.   return ret;
  1494. }
  1495.  
  1496. /* Print file name */
  1497. static void PrintCHKXFile(struct FileData *fd)
  1498. {
  1499.   STRPTR name = fd->fd_Name;
  1500.   UBYTE i;
  1501.  
  1502.   if(fd->fd_Flags & CHECKXFLAG_DEBUG)
  1503.   {
  1504.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1505.       KPutC('*');
  1506.     KPrintf("%s\n", name);
  1507.   }
  1508.  
  1509.   if(fd->fd_LogFileFH)
  1510.   {
  1511.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1512.       FPutC(fd->fd_LogFileFH, '*');
  1513.     FPrintf(fd->fd_LogFileFH, "%s\n", name);
  1514.   }
  1515.   if(!(fd->fd_Flags & CHECKXFLAG_QUIET))
  1516.   {
  1517.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1518.       FPutC(Output(), '*');
  1519.     Printf("%s\n", name);
  1520.   }
  1521.   fd->fd_Flags |= CHECKXFLAG_NAMEPRINTED;
  1522. }
  1523.  
  1524. static void PrintCHKXErr(struct FileData *fd, LONG err)
  1525. {
  1526.   if(err)
  1527.   {
  1528.     STRPTR txt = 0, txt2;
  1529.  
  1530.     if(err > XFDERR_OFFSET)
  1531.     {
  1532.       ++fd->fd_XFDErrors;
  1533.       txt2 = "XFD-Error %ld: %s";
  1534.       err -= XFDERR_OFFSET;
  1535.       txt = xfdGetErrorText(err);
  1536.     }
  1537.     else if(err > XADERR_OFFSET)
  1538.     {
  1539.       ++fd->fd_XADErrors;
  1540.       txt2 = "XAD-Error %ld: %s";
  1541.       err -= XADERR_OFFSET;
  1542.       txt = xadGetErrorText(err);
  1543.     }
  1544.     else if(err > CHXWARN_OFFSET)
  1545.     {
  1546.       ++fd->fd_CHKXWarnings;
  1547.       txt2 = "CheckX-Warning %ld: %s";
  1548.       switch(err)
  1549.       {
  1550.       case CHKXWARN_NOVIRUS:    txt = "Virus-Checking disabled!";                        break;
  1551.       case CHKXWARN_XVSSELFTEST:txt = "The xvs.library is modified, maybe the system is virus infected!";    break;
  1552.       case CHKXWARN_MEMVIRUS:    txt = "Your system memory was virus infected!";                    break;
  1553.       case CHKXWARN_EMPTY:    txt = "file is empty";                                break;
  1554.       }
  1555.       err -= CHXWARN_OFFSET;
  1556.     }
  1557.     else
  1558.     {
  1559.       ++fd->fd_CHKXErrors;
  1560.       txt2 = "CheckX-Error %ld: %s";
  1561.       switch(err)
  1562.       {
  1563.       case CHKXERR_NOMEMORY:    txt = "not enough memory";            break;
  1564.       case CHKXERR_EXAMINEERR:    txt = "examining failed";            break;
  1565.       case CHKXERR_OPENERR:    txt = "opening file failed";            break;
  1566.       case CHKXERR_READ:    txt = "reading failed";                break;
  1567.       case CHKXERR_SCANERR:    txt = "directory scan failed";            break;
  1568.       case CHKXERR_BREAK:    txt = "user break";                break;
  1569.       case CHKXERR_OPENDIR:    txt = "opening directory failed";        break;
  1570.       case CHKXERR_RESOURCE:    txt = "needed resource not available";        break;
  1571.       case CHKXERR_NOBOOTVIRUS:    txt = "could not check for bootblock virus";    break;
  1572.       case CHKXERR_WRITE:    txt = "writing failed";                break;
  1573.       case CHKXERR_NOVIRUS:    txt = "could not check for virus";        break;
  1574.       case CHKXERR_NOMEMORYARC:    txt = "not enough memory for full tests";    break;
  1575.       case CHKXERR_NOSECTOR:    txt = "could not check sectors";        break;
  1576.       }
  1577.     }
  1578.  
  1579.     PrintCHKXTxt(fd, txt2, err, txt);
  1580.   }
  1581. }
  1582.  
  1583. /* Print type text */
  1584. static void PrintCHKXTxt(struct FileData *fd, STRPTR txt, ...)
  1585. {
  1586.   UBYTE i;
  1587.  
  1588.   if(!(fd->fd_Flags & CHECKXFLAG_NAMEPRINTED))
  1589.     PrintCHKXFile(fd);
  1590.  
  1591.   if(fd->fd_Flags & CHECKXFLAG_DEBUG)
  1592.   {
  1593.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1594.       KPutC(' ');
  1595.     RawDoFmt(txt, &txt+1, (void (*)()) KPutC, 0);
  1596.     KPutC('\n');
  1597.   }
  1598.  
  1599.   if(fd->fd_LogFileFH)
  1600.   {
  1601.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1602.       FPutC(fd->fd_LogFileFH, ' ');
  1603.     VFPrintf(fd->fd_LogFileFH, txt, &txt+1);
  1604.     FPutC(fd->fd_LogFileFH, '\n');
  1605.     Flush(fd->fd_LogFileFH);
  1606.   }
  1607.  
  1608.   if(!(fd->fd_Flags & CHECKXFLAG_QUIET))
  1609.   {
  1610.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1611.       FPutC(Output(), ' ');
  1612.     VPrintf(txt, &txt+1);
  1613.     FPutC(Output(), '\n');
  1614.   }
  1615. }
  1616.  
  1617. /* Add memory to the memory list. */
  1618. static LONG AddCrunchMemList(struct FileData *fd, APTR reg, ULONG size)
  1619. {
  1620.   struct CrunchMemList *ml;
  1621.  
  1622.   if((ml = (struct CrunchMemList *) AllocMem(sizeof(struct CrunchMemList),
  1623.   MEMF_ANY)))
  1624.   {
  1625.     ml->cml_Next = fd->fd_MemList;
  1626.     ml->cml_MemoryRegion = reg;
  1627.     ml->cml_MemorySize = size;
  1628.     fd->fd_MemList = ml;
  1629.     return 0;
  1630.   }
  1631.  
  1632.   return CHKXERR_NOMEMORY;
  1633. }
  1634.  
  1635. /* Free memory from the memory list. */
  1636. static void FreeCrunchMemList(struct FileData *fd, APTR reg)
  1637. {
  1638.   struct CrunchMemList mc, *ml = &mc;
  1639.  
  1640.   if(fd->fd_Flags & CHECKXFLAG_NOFREEMEM)
  1641.     return;
  1642.  
  1643.   for(mc.cml_Next = fd->fd_MemList; ml; ml = ml->cml_Next)
  1644.   {
  1645.     if(ml->cml_Next->cml_MemoryRegion == reg)
  1646.     {
  1647.       struct CrunchMemList *m;
  1648.       m = ml->cml_Next;
  1649.       ml->cml_Next = m->cml_Next;
  1650.       FreeMem(m->cml_MemoryRegion, m->cml_MemorySize);
  1651.       FreeMem(m, sizeof(struct CrunchMemList));
  1652.     }
  1653.   }
  1654.  
  1655.   fd->fd_MemList = mc.cml_Next;
  1656. }
  1657.  
  1658. /* Save file, when SAVE option and file was crunched or linked. */
  1659. static LONG SaveUncrFile(struct FileData *fd, APTR buf, ULONG size)
  1660. {
  1661.   BPTR filefh, cd;
  1662.   LONG ret = 0, i = 0;
  1663.   UBYTE name[50];
  1664.   UBYTE nbuf[256];
  1665.  
  1666.   if(!(fd->fd_SaveDirL && (fd->fd_Flags & CHKXSAVEFLAGS)))
  1667.     return 0;
  1668.  
  1669.   SPrintF(name, (fd->fd_LinkNum ? "%s.%ld" : "%s"), FilePart(fd->fd_Name),
  1670.   fd->fd_LinkNum++);
  1671.  
  1672.   cd = CurrentDir(fd->fd_SaveDirL);
  1673.  
  1674.   while(!ret && name[i])
  1675.   {
  1676.     for(;name[i] && name[i] != '/'; ++i)
  1677.       nbuf[i] = name[i];
  1678.     if(name[i] == '/')
  1679.     {
  1680.       nbuf[i] = 0;
  1681.       if((filefh = Lock(nbuf, SHARED_LOCK)))
  1682.         UnLock(filefh);
  1683.       else
  1684.       {
  1685.         if((filefh = CreateDir(nbuf)))
  1686.           UnLock(filefh);
  1687.         else
  1688.           ret = CHKXERR_OPENERR;
  1689.       }
  1690.       nbuf[i] = name[i];
  1691.       ++i;
  1692.     }
  1693.   }
  1694.  
  1695.   if(!ret)
  1696.   {
  1697.     if((filefh = Open(name, MODE_NEWFILE)))
  1698.     {
  1699.       if(Write(filefh, buf, size) != size)
  1700.         ret = CHKXERR_WRITE;
  1701.       Close(filefh);
  1702.     }
  1703.     else
  1704.       ret = CHKXERR_OPENERR;
  1705.   }
  1706.  
  1707.   CurrentDir(cd);
  1708.  
  1709.   return ret;
  1710. }
  1711.  
  1712. #ifdef __SASC
  1713.   #undef SysBase
  1714.   #define SysBase sysbase
  1715. #endif
  1716.  
  1717. static ASM(LONG) BreakHookCheckX(void)
  1718. {
  1719.   return (SetSignal(0L,0L) & SIGBREAKF_CTRL_C) ? 0 : XADPIF_OK;
  1720. }
  1721. static const struct Hook breakhook = {{0,0},(ULONG (*)()) BreakHookCheckX, 0};
  1722.  
  1723. static ASM(void) SPrintF_putfunc(REG(d0, UBYTE data), REG(a3, STRPTR *a))
  1724. {
  1725.   *((*a)++) = data;
  1726. }
  1727.  
  1728. static void SPrintF(STRPTR buf, STRPTR format, ...)
  1729. {
  1730.   STRPTR buf2 = buf;
  1731.   RawDoFmt(format, (APTR) ((ULONG)&format+sizeof(STRPTR)),
  1732.   (void(*)()) SPrintF_putfunc, &buf2);
  1733. }
  1734.  
  1735. static ASM(void) KPutC(REG(d0, ULONG c))
  1736. {
  1737.   RawPutChar(c);
  1738. }
  1739.  
  1740. static void KPrintf(STRPTR fmt, ...)
  1741. {
  1742.   RawDoFmt(fmt, &fmt + 1, (void (*)()) KPutC, 0);
  1743. }
  1744.  
  1745.